using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MSIACCSL2.ViewModels;
using WcfService1.DTO;

namespace MSIACCSL2.Views.SubGroup
{
    public partial class SearchSubgroup : ChildWindow
    {
        public SubGroupViewModel ViewModel
        {
            get
            {
                return this.DataContext as SubGroupViewModel;
            }
        }

        private bool _isInitialLoad = true;
        public SearchSubgroup()
        {
            InitializeComponent();
            SubGroupViewModel.OnSearchCompleted = (o) =>
            {
                this.DataContext = (SubGroupViewModel)o;
            };
            this.Loaded += new RoutedEventHandler(SearchSubgroup_Loaded);
            this.txtSearch.KeyDown +=new KeyEventHandler(txtSearch_KeyDown);
            this.dg1.KeyUp += new KeyEventHandler(dg1_KeyUp);
            this.GotFocus += new RoutedEventHandler(SearchSubgroup_GotFocus);
            this.KeyUp += (o, e) =>
                              {
                                  if (e.Key == Key.Escape)
                                      this.Close();
                                  ;
                              };

        }

        void SearchSubgroup_GotFocus(object sender, RoutedEventArgs e)
        {
            if (_isInitialLoad)
            {
                txtSearch.Focus();
                _isInitialLoad = false;
            }
        }

        void dg1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (dg1.SelectedIndex != 0) dg1.SelectedIndex -= 1;
                Close();
            }
            else if (e.Key == Key.Escape)
            {
                txtSearch.Focus();
            }
        }

     
        void SearchSubgroup_Loaded(object sender, RoutedEventArgs e)
        {
            _isInitialLoad = true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void txtSearch_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	if (e.Key==Key.Enter)
        	{
                ViewModel.GetSubGroupByName((sender as TextBox).Text);
        	}else if(e.Key == Key.Down)
        	{
        	    dg1.Focus();
        	}
        }

        private void dg1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (dg1.SelectedItem != null)
            {
                txtSearch.Text = ((SubGroupDTO)dg1.SelectedItem).SubGroupName;
            }
           
        }

        private void txtSearch_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
           txtSearch.SelectAll();
        }
    }
}

